package com.zhengqiangblog.blog.service;

import org.apache.log4j.Logger;

import com.zhengqiangblog.blog.model.Pagination;

public class PaginationCalculator {

	private static final Logger logger = Logger.getLogger(PaginationCalculator.class);

	public PaginationCalculator() {
		logger.info("PaginationCalculator initializing...");
	}

	public Pagination calculate(int pageNo, int totalItems) {
		Pagination pagination = new Pagination();
		pagination.setPageNo(pageNo);
		int defaultPageSize = Pagination.DEFAULT_PAGE_SIZE;
		pagination.setTotalItems(totalItems);
		int totalPages = totalItems / defaultPageSize;
		int mod = totalItems % defaultPageSize;
		if (mod > 0) {
			totalPages = totalPages + 1;
		}
		if (totalPages == 0) {
			totalPages = 1;
		}
		pagination.setTotalPages(totalPages);
		return pagination;
	}

}
